package com.jpcc.vw.onlineRadio.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cns.android.atmospherelamp.AtmosphereLampManager;
import com.jpcc.vw.onlineRadio.R;
import com.jpcc.vw.onlineRadio.base.BaseActivity;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.model.IRadio;
import com.jpcc.vw.onlineRadio.model.bean.BaseBean;
import com.jpcc.vw.onlineRadio.model.bean.CategoryTagBean;
import com.jpcc.vw.onlineRadio.model.bean.CategoryTagEntity;
import com.jpcc.vw.onlineRadio.model.impl.RadioImpl;
import com.jpcc.vw.onlineRadio.utils.CommonSubscriber;
import com.jpcc.vw.onlineRadio.utils.ToastUtils;
import com.jpcc.vw.onlineRadio.utils.ViewHolderUtils;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action0;

public class CategoryTagActivity extends BaseActivity {

    @BindView(R.id.layout_back)
    RelativeLayout layout_back;
    @BindView(R.id.recyclerview_category_tag)
    ListView recyclerview_category_tag;
    @BindView(R.id.layout_search)
    RelativeLayout layout_search;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_show_empty)
    TextView tv_show_empty;
    @BindView(R.id.imv_top_bottom_line)
    ImageView imv_top_bottom_line;
    @BindView(R.id.progress_circular)
    ProgressBar progress_circular;
    @BindView(R.id.imv_new_search)
    ImageView imv_new_search;

    private IRadio model;
    private long categoryId;
    private String categoryName;
    private List<CategoryTagEntity> datas = new ArrayList<>();
    private TagAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_category_tag;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        layout_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CategoryTagActivity.this.finish();
            }
        });
        imv_new_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_search = new Intent(CategoryTagActivity.this, SearchActivity.class);
                startActivity(intent_search);
            }
        });
        categoryId = getIntent().getLongExtra("KEY_CATEGORY_ID", -1);
        categoryName = getIntent().getStringExtra("KEY_CATEGORY_NAME");
        if (categoryId == -1) {
            this.finish();
        }
        tv_title.setText(categoryName);
        adapter = new TagAdapter();
        recyclerview_category_tag.setAdapter(adapter);
        getCategoryTag();
    }

    @Override
    public void setupData() {
        if (Constants.isRelease) {
            try {
                boolean atmosphereStatus = AtmosphereLampManager.getInstance(this).isHmiAtmosphereLampOn();
                String color = AtmosphereLampManager.getInstance(this).getHmiAtmosphereLampColor();
                doAtmosphere(atmosphereStatus, color);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void doAtmosphere(boolean rgbOn, String color) {
        Logger.i("doAtmosphere" + "rgbOn-->" + rgbOn);
        Logger.i("doAtmosphere" + "color-->" + color);
        if (rgbOn) {
            if (TextUtils.isEmpty(color)) {
                imv_top_bottom_line.setColorFilter(Color.TRANSPARENT);
            } else {
                imv_top_bottom_line.setColorFilter(Color.parseColor(color));
            }
        } else {
            imv_top_bottom_line.setColorFilter(Color.TRANSPARENT);
        }
    }

    private void getCategoryTag() {
        if (model == null) {
            model = new RadioImpl();
        }
        model.GetCategoryTags(categoryId).doOnSubscribe(new Action0() {
            @Override
            public void call() {
//                showProgress();
                progress_circular.setVisibility(View.VISIBLE);
            }
        }).subscribe(new CommonSubscriber<CategoryTagBean>() {
            @Override
            public void onSuccess(CategoryTagBean response) {
//                hideProgress();
                progress_circular.setVisibility(View.INVISIBLE);
                if (response.getData() != null && response.getData().size() > 0) {
                    tv_show_empty.setVisibility(View.GONE);
                    datas.clear();
                    datas.addAll(response.getData());
                    adapter.notifyDataSetChanged();
                    recyclerview_category_tag.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(CategoryTagActivity.this, DailyRecommendationActivity.class);
                            intent.putExtra("KEY_TYPE", 2);
                            intent.putExtra("KEY_CATEGORY_ID", categoryId);
                            intent.putExtra("KEY_CATEGORY_TAG", datas.get(position).getTagName());
                            startActivity(intent);
                        }
                    });
                } else {
                    tv_show_empty.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(BaseBean response) {
//                hideProgress();
                progress_circular.setVisibility(View.INVISIBLE);
                ToastUtils.show(response.getStatusMessage());
                tv_show_empty.setVisibility(View.VISIBLE);
            }
        });
    }

    public class TagAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_album_category, parent, false);
            }
            initViews(convertView, position);
            return convertView;
        }
    }

    private void initViews(View view, int position) {
        TextView tv_category = ViewHolderUtils.get(view, R.id.tv_category);
        TextView tv_position = ViewHolderUtils.get(view, R.id.tv_position);
        ImageView imv_cover = ViewHolderUtils.get(view, R.id.imv_cover);
        tv_category.setText(datas.get(position).getTagName());
        tv_position.setText(position + 1 + "");
        imv_cover.setVisibility(View.GONE);
    }
}
